/* Copyright (c) 2009 - 2010 98Labs Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sbal89.map.util;

import java.util.List;

import com.google.gdata.data.spreadsheet.Cell;
import com.google.gdata.data.spreadsheet.CellEntry;

public class XMLUtil {
	
	public static String getPovinces(List<CellEntry> cellEntries, int rowStart) {
		String xml = "";
		String rowString = "";
		int rowCount = rowStart;
		int rowLabel = 0;
		for (CellEntry cellEntry: cellEntries) {
			Cell cell = cellEntry.getCell();
			int cellCol = cell.getCol();
			int cellRow = cell.getRow();
			rowLabel =cellRow-2;
			if (rowCount<cellRow) {
				//if rows doesn't match then print row string if it's not empty
				if (rowString.length()>0) {
					xml += "\t<province name=\"P"+rowLabel+"\" " + rowString + "/>\n";
					rowCount=cellRow; //jump to where the cell row is
					rowString = ""; //reset row string
				}
			}
			//collect values for row string
			rowString += "C"+(cellCol-1)+"=\"" + cell.getValue() +"\" ";
		}
		if (rowString.length()>0) {
			// if row string have unprinted contents then print it
			xml += "\t<province name=\"P"+(rowLabel+1)+"\" " + rowString + "/>\n";
		}
		return xml;
	}
}
